<template>
  <el-card class="doc-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新增文档地址</el-button>
      </div>
    </template>
    <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="文档ID"
      ></el-table-column>
      <el-table-column
          prop="name"
          label="文档名"
      >
      </el-table-column>
      <el-table-column
          prop="introduction"
          label="文档简介"
      >
      </el-table-column>
      <el-table-column
          prop="url"
          label="文档地址"
          width="300px"
      >
        <template #default="scope">
          <a :href="scope.row.url">{{ scope.row.url }}</a>
        </template>
      </el-table-column>


      <el-table-column
          label="操作"
          width="100"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="changePage"
    />
  </el-card>
</template>

<script>
import {onMounted, reactive, ref, toRefs} from 'vue'
import {ElMessage} from 'element-plus'
import {useRouter} from 'vue-router'
import api from "@/api/docs";

export default {
  name: 'Good',
  setup() {
    const multipleTable = ref(null)
    const router = useRouter()
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10 // 分页大小
    })
    onMounted(() => {
      getDocList()
    })
    // 获取轮播图列表
    const getDocList = () => {
      state.loading = true
      api.docs({
            pageNumber: state.currentPage,
            pageSize: state.pageSize
          }
      ).then(res => {
        state.tableData = res.records
        state.total = res.total
        state.currentPage = res.current
        state.loading = false
      })
    }
    const handleAdd = () => {
      router.push({path: '/add'})
    }
    const handleEdit = (id) => {
      router.push({path: '/add', query: {id}})
    }
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
    const changePage = (val) => {
      state.currentPage = val
      getDocList()
    }
    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      handleAdd,
      handleEdit,
      getDocList,
      changePage
    }
  }
}
</script>

<style scoped>
.doc-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>